#include <iostream>
#include <unistd.h>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>


class tcpserver;

struct pthreadDate
{
    pthreadDate(int fd, const std::string& ip, uint16_t port, tcpserver* s)
        :_sockfd(fd), _clientip(ip), _clientport(port), _server(s)
    { }
    int _sockfd;
    std::string _clientip;
    uint16_t _clientport;
    tcpserver* _server;
};

class tcpserver
{
public:
    tcpserver(uint16_t port, const std::string &ip = "0.0.0.0") : _listenfd(0), _port(port), _serverip(ip)
    {
    }
    void init()
    {
        _listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if (_listenfd < 0)
        {
            std::cout << "socket error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
            ;
            exit(0);
        }
        std::cout << "socket over..." << std::endl;
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(_serverip.c_str());
        server.sin_port = htons(_port);
        if (bind(_listenfd, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            std::cout << "bind error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
            exit(0);
        }
        std::cout << "bind over..." << std::endl;
        if (listen(_listenfd, 10) < 0)
        {
            std::cout << "listen error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
            exit(0);
        }
        std::cout << "listen over..." << std::endl;
    }
    static void* pthreadServer(void* arg)
    {
        pthread_detach(pthread_self());
        pthreadDate* data = (pthreadDate*)arg;
        data->_server->server(data->_sockfd, data->_clientip, data->_clientport);
        close(data->_sockfd);
        delete data;
        return nullptr;
    }
    void run()
    {
        struct sockaddr_in client;
        socklen_t len = sizeof(client);
        while (true)
        {
            int sockfd = accept(_listenfd, (struct sockaddr *)&client, &len);
            uint16_t clientport;
            std::string clientip;
            clientport = ntohs(client.sin_port);
            clientip = inet_ntoa(client.sin_addr);

            if (sockfd < 0)
            {
                std::cout << "accept error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
                exit(0);
            }
            std::cout << "accept over..." << "clientip:" << clientip << ":" << std::to_string(clientport) << std::endl;

            //----------version 1-----------//
            // pid_t id = fork();
            // if (id == 0)
            // {
            //     close(_listenfd);
            //     if (fork() > 0)
            //         exit(0);
            //     server(sockfd, clientip, clientport);
            //     close(sockfd);
            //     exit(0);
            // }
            // close(sockfd);
            // waitpid(id, nullptr, 0);

            //----------version 2-----------//
            pthread_t tid;
            pthreadDate* arg = new pthreadDate(sockfd, clientip, clientport, this);
            if(pthread_create(&tid, nullptr, pthreadServer, (void*)arg) < 0)
            {
                std::cout << "pthread create error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
                continue;
            }
        }
    }
    
    void server(int sockfd, const std::string &ip, uint16_t port)
    {
        char buffer[1024]{0};
        while (true)
        {
            int n = read(sockfd, buffer, sizeof(buffer));
            if (n == 0)
            {
                std::cout << "client close, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
                break;
            }
            if (n < 0)
            {
                std::cout << "read error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
                break;
            }
            buffer[n] = 0;
            std::string serversay = ip + ":";
            serversay += std::to_string(port);
            serversay += "# ";
            serversay += buffer;
            n = write(sockfd, serversay.c_str(), serversay.size());
            if (n < 0)
            {
                std::cout << "write error, errno:" << errno << ", errerstr:" << strerror(errno) << std::endl;
                break;
            }
        }
    }
    ~tcpserver()
    {
    }

private:
    int _listenfd;
    std::string _serverip;
    uint16_t _port;
};